﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using Wicresoft.Framework.Organization;
using Wicresoft.Busi.Entity;
using Wicresoft.Busi.BizLogic;
using Wicresoft.Common;

public partial class Car_VehicleApplyQuery : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 事由";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "return searchClick();");

        if (!OrganizationHelper.IsUserInRole(CurrentUser.ID, Wicresoft.Busi.Entity.VehicleRoles.Manage))
        {
            Response.Write("您无权查看此页面,请使用<a href='MyVehicleApply.aspx'>我的申请</a>");
            Response.End();
        }
        if (!IsPostBack)
        {
            initUseStatus();
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ApplyDate";
            this.Order.OrderDirection = "DESC";

            BindData();
        }

    }

    private void initUseStatus()
    {
        DataTable dt = BLDic_Vehicle.GetUseStatus();
        this.ddlStatus.DataSource = dt;
        this.ddlStatus.DataTextField = Wicresoft.Busi.Entity.Dic_Vehicle.SQLCOL_PROPERTYNAME;
        this.ddlStatus.DataValueField = Wicresoft.Busi.Entity.Dic_Vehicle.SQLCOL_PROPERTYID;
        this.ddlStatus.DataBind();
        ListItem item = new ListItem("- 所有 -", "");
        this.ddlStatus.Items.Insert(0, item);
    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;
        DTData = DataAccess.Select(typeof(Wicresoft.JDPostBoard.Entity.VehicleApply2), Pager.PageIndex, Pager.PageSize, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        DataTable dtStatus = BLDic_Vehicle.GetUseStatus();
        foreach (DataRow dr in DTData.Rows)
        {
            foreach (DataRow drStatus in dtStatus.Rows)
            {
                if (dr["Status"].ToString() == drStatus["PropertyID"].ToString())
                    dr["Distance"] = drStatus["PropertyName"].ToString();
            }
        }

        Pager.RecordCount = recordCount;
        this.rptOrder.DataSource = DTData;
        this.rptOrder.DataBind();
    }

    protected void rptOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //switch (e.CommandName)
        //{
        //    case "Delete":
        //        OrderList ol = new OrderList(int.Parse(e.CommandArgument.ToString()));
        //        ol.Load();
        //        if (ol.Status == "申购提交")
        //        {
        //            ol.Status = "取消申购";
        //            ol.Save();
        //        }
        //        BindData();
        //        break;

        //}
    }

    protected void rptOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchClause = " 1=1 ";
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", Wicresoft.JDPostBoard.Entity.VehicleApply2.SQLCOL_PURPOSE, tbSearch.Text.Replace("'", "''").Replace("%", "[%]"));
        BindData();

    }

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClause = " 1=1 ";

        if (this.txtPurpose.Text != string.Empty)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", Wicresoft.JDPostBoard.Entity.VehicleApply2.SQLCOL_PURPOSE, txtPurpose.Text.Replace("'", "''").Replace("%", "[%]"));
        if (this.txtStartDate.Text != string.Empty)
            SearchClause += string.Format(" AND {0}>='{1}' AND {0}<'{2}'", Wicresoft.JDPostBoard.Entity.VehicleApply2.SQLCOL_STARTDATE, txtStartDate.Text, Convert.ToDateTime(this.txtStartDate.Text).AddDays(1).ToString());
        if (this.ddlStatus.SelectedValue != "")
            SearchClause += string.Format(" AND {0} = N'{1}'", Wicresoft.JDPostBoard.Entity.VehicleApply2.SQLCOL_STATUS, this.ddlStatus.SelectedValue);

        BindData();
    }
}
